/**
 * @RpcProxyFactoryBean.java  COPYRIGHT xiaojian
 * 
 * Created on 2017年1月6日 上午11:27:57
 */
package xj.toolkit.rpc.client;

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

import xj.toolkit.rpc.naming.RpcNamingService;

/**
 * @author <a href="mailto:cxj2000@gmail.com">xiaojian.cao</a>
 *
 */
public class RpcProxyFactoryBean implements FactoryBean<Object>, InitializingBean {

	private Class<?> proxyInterface;

	private Object rpcProxyBean;

	private RpcNamingService namingService = null;

	private String serviceName = null;

	public Class<?> getProxyInterface() {
		return proxyInterface;
	}

	public void setProxyInterface(Class<?> proxyInterface) {
		this.proxyInterface = proxyInterface;
	}

	public RpcNamingService getNamingService() {
		return namingService;
	}

	public void setNamingService(RpcNamingService namingService) {
		this.namingService = namingService;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	@Override
	public Object getObject() throws Exception {
		return this.rpcProxyBean;
	}

	@Override
	public Class<?> getObjectType() {
		return this.proxyInterface;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		ClientMethodInterceptor interceptor = new ClientMethodInterceptor(this.serviceName, this.namingService,
				this.proxyInterface);

		this.rpcProxyBean = new ProxyFactory(this.proxyInterface, interceptor).getProxy();
	}

}
